<template>
    <div class="sort-box">
        <span class="sort-title">{{column.title}}</span>
        <span class="sort-icon-box">
            <i class="sort-caret ascending" @click.stop="handleSortChange('asc',$event)"></i>
            <i class="sort-caret descending" @click.stop="handleSortChange('desc',$event)"></i>
        </span>
    </div>
</template>

<script>
export default {
    name: 'sort',

    inject: ['$pageTable'],

    props: {
        column: {
            type: Object,
            default() {
                return {};
            }
        }
    },

    methods: {
        handleSortChange(value, event) {
            if (!event.target.classList.contains('active')) {
                const $el = className => {
                    return this.$pageTable.$refs.table.$el.querySelectorAll(className);
                };

                Array.from($el('.sort-icon-box .sort-caret')).forEach(item => {
                    item.classList.remove('active');
                });

                event.target.classList.add('active');
                this.$pageTable.$refs.table.sort(this.column.property, value);
            }
        }
    }
};
</script>

<style lang="scss" scoped>
$defultColor: #c0c4cc;
$color: #409eff;

.sort-box {
    display: flex;
    align-items: center;
    justify-content: center;
    .sort-title {
        color: #909399;
        text-align: center;
        font-size: 12px;
    }
    .sort-icon-box {
        display: inline-flex;
        -webkit-box-orient: vertical;
        -webkit-box-direction: normal;
        flex-direction: column;
        -webkit-box-align: center;
        align-items: center;
        -webkit-box-pack: justify;
        justify-content: space-between;
        height: 22px;
        width: 16px;
        vertical-align: middle;
        cursor: pointer;
        overflow: initial;
        position: relative;
        .sort-caret {
            width: 0;
            height: 0;
            border: 5px solid transparent;
            &.ascending {
                border-bottom-color: $defultColor;
                &.active {
                    border-bottom-color: $color;
                }
            }
            &.descending {
                border-top-color: $defultColor;
                &.active {
                    border-top-color: $color;
                }
            }
        }
    }
}
</style>